package com.bianguo.android.apushdemo.view.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.events.EmptyEvent;
import com.bianguo.android.apushdemo.utils.SharePreferenceUtil;
import com.gyf.barlibrary.ImmersionBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/7/31.
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected Unbinder unBind;
    protected Activity mContext;
    protected SharePreferenceUtil spu;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        mContext = this;
        spu = SharePreferenceUtil.getInstance(mContext);
        int theme = spu.getTheme();
        if (0 == theme) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else if (1 == theme) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
        setStatusColor(ActivityCompat.getColor(mContext, R.color.navigationview_bg_color));
//        ImmersionBar.with(this)
//                .transparentBar()
//                .statusBarColor(R.color.navigationview_bg_color)
//                .statusBarAlpha(0f)
//                .init();
        setContentView(layoutId());
        unBind = ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initView(savedInstanceState);
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public void setStatusColor(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(color);//设置状态栏颜色代码
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EmptyEvent event){

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unBind.unbind();
    }

    public abstract int layoutId();

    public abstract void initView(@Nullable Bundle savedInstanceState);
}
